﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace FurnitureSystem
{
    public partial class frmQuanLySP : Form
    {
        SqlConnection conn;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        public static string masp1;
        private int IDD;
        public frmQuanLySP()
        {
            InitializeComponent();
            conn = connect.Getconect();
        }
        private void frmQuanLySP_Load(object sender, EventArgs e)
        {
            if (frmLogin.depart == "admin     ") {
                BtnThem.Enabled = true;
                btnSua.Enabled = true;
                btnXoa.Enabled = true;
            }

            LoadlstLoai();
        }

        private void LoadlstLoai()
        {
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("select * from Type", conn);
            DataSet ds = new DataSet("type");
            da.Fill(ds);
            lstLoai.DataSource = ds.Tables[0];
            lstLoai.DisplayMember = ds.Tables[0].Columns[1].ToString();
            lstLoai.ValueMember = ds.Tables[0].Columns[0].ToString();
            
        }
        //private void LoaddgChitiet()
        //{
        //    this.IDD = Int32.Parse(lstLoai.SelectedValue.ToString());
        //    conn = connect.Getconect();
        //    conn.Open();
        //    string sql = "select * from Product where Typeid=" + IDD + "";
        //    MessageBox.Show(sql);
        //    cmd = new SqlCommand(sql, conn);
        //    da = new SqlDataAdapter(sql, conn);
        //    SqlCommandBuilder scmb = new SqlCommandBuilder(da);
        //    DataTable dt = new DataTable();
        //    dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
        //    da.Fill(dt);
        //    dt.Columns["Productid"].ColumnName = "Mã sản phẩm";
        //    dt.Columns["Productname"].ColumnName = "Tên sản phẩm";
        //    dt.Columns["Unitprice"].ColumnName = "Giá sản phẩm";
        //    dt.Columns["Quantity"].ColumnName = "Số lượng";
        //    dt.Columns["Typeid"].ColumnName = "Mã loại SP";
        //    dgChitietSP.DataSource = dt;
        //    int i = dgChitietSP.RowCount - 1;
        //    lbSohang.Text = i.ToString();
        //    conn.Close();
        //    LoaddgChitiet();

        //}

        //private void lstLoai_SelectedValueChanged(object sender, EventArgs e)
        //{
        //    LoaddgChitiet();
        //}

        private void btnChitiet_Click(object sender, EventArgs e)
        {
            IDD = Convert.ToInt32(lstLoai.SelectedValue);
            conn = connect.Getconect();
            conn.Open();
            string sql = "select * from Product where Typeid=" + IDD + "";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(sql, conn);
            SqlCommandBuilder scmb = new SqlCommandBuilder(da);
            DataTable dt = new DataTable();
            dt.Locale = System.Globalization.CultureInfo.InvariantCulture;
            da.Fill(dt);
            dt.Columns["Productid"].ColumnName = "Mã sản phẩm";
            dt.Columns["Productname"].ColumnName = "Tên sản phẩm";
            dt.Columns["Unitprice"].ColumnName = "Giá sản phẩm";
            dt.Columns["Quantity"].ColumnName = "Số lượng";
            dt.Columns["Typeid"].ColumnName = "Mã loại SP";
            dgChitietSP.DataSource = dt;
            int i = dgChitietSP.RowCount - 1;
            lbSohang.Text = i.ToString();
            conn.Close();
            
        }

        private void BtnThoat_Click(object sender, EventArgs e)
        {
            frmMain frmMain = new frmMain();
            frmMain.Show();
            this.Hide();
        }

        private void BtnThem_Click(object sender, EventArgs e)
        {
            frmThemSP frmThemSP = new frmThemSP();
            frmThemSP.Show();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            
            string masp = dgChitietSP.CurrentRow.Cells[0].FormattedValue.ToString();
            string tensp = dgChitietSP.CurrentRow.Cells[1].FormattedValue.ToString();
            string giasp = dgChitietSP.CurrentRow.Cells[2].FormattedValue.ToString();
            string slsp = dgChitietSP.CurrentRow.Cells[3].FormattedValue.ToString();
            string maloaisp = dgChitietSP.CurrentRow.Cells[4].FormattedValue.ToString();
            conn = connect.Getconect();
            conn.Open();
            string sql = "update Product set Productid='" + masp + "',Productname='" + tensp + "',Unitprice='" + Int32.Parse(giasp) + "',Quantity='" + Int32.Parse(slsp) + "',Typeid='" + Int32.Parse(maloaisp) + "' where Productid='" + masp1 + "'";

            cmd = new SqlCommand(sql, conn);
            dr = cmd.ExecuteReader();
            
            conn.Close();



        }

        private void dgChitietSP_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            masp1 = dgChitietSP.CurrentRow.Cells[0].FormattedValue.ToString();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            string masp2 = dgChitietSP.CurrentRow.Cells[0].FormattedValue.ToString();
            conn = connect.Getconect();
            conn.Open();
            string sql = "delete from Product where Productid = '" +masp2+ "'";
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter(cmd);
            dr = cmd.ExecuteReader();
            
            conn.Close();
            

        }

    }
}
